# -*- coding: utf-8 -*-

from django.db import models
from django.utils.translation import ugettext_lazy as _

class Country(models.Model):
    name = models.CharField(max_length=50)
    
    class Meta:
      ordering = ['name']
      verbose_name_plural = 'Countries'
    
    def __unicode__(self):
      return self.name

class Region(models.Model):
    country = models.ForeignKey(Country)
    name = models.CharField(max_length=30)
    
    class Meta:
      unique_together = (('name', 'country'),)
      ordering = ['name']
    
    def __unicode__(self):
      return self.name

class City(models.Model):
    region = models.ForeignKey(Region)
    name = models.CharField(max_length=60)

    class Meta:
      unique_together = (('name', 'region'),)
      ordering = ['name']
      verbose_name_plural = 'Cities'
    
    def __unicode__(self):
      return self.name
  
def get_regions_of_country(country):
    try:
        c = Country.objects.get(pk=country)
        return Region.objects.all().filter(country=c)
    except Country.DoesNotExist:
        return []
    except Region.DoesNotExist:
        return []

def get_cities_of_region(region):
    try:
        s = Region.objects.get(pk=region)
        return City.objects.all().filter(region=s)
    except Region.DoesNotExist:
        return []
    except City.DoesNotExist:
        return []
    
    